using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AutoShuntingSchedule.Utility.Extension;
using JinianNet.JNTemplate;
using SqlSugar;

namespace AutoShuntingSchedule.DAL
{
    public class BaseLogic
    {
        private static string ConnectionString;
        private static SqlSugar.DbType DbType;
        private static string DbName;
        private static bool DebugMode;

        public static bool InitDB(string dbType, string host, string dbName, string userName, string pasword, ref string message, bool debug = false)
        {
            DebugMode = debug;
            DbName = dbName;
            if (dbType.ToLower() == "SqlServer".ToLower())
            {
                DbType = SqlSugar.DbType.SqlServer;
                ConnectionString = $"Data Source={host};Initial Catalog={dbName};User ID={userName};Password={pasword};";
                return true;
            }
            else if (dbType.ToLower() == "MySql".ToLower())
            {
                DbType = SqlSugar.DbType.MySql;
                ConnectionString = $"server={host};Database={dbName};Uid={userName};Pwd={pasword};";
                return true;
            }
            else if (dbType.ToLower() == "Oracle".ToLower())
            {
                DbType = SqlSugar.DbType.Oracle;
                ConnectionString = $"Data Source={host}/{dbName};User ID={userName};Password={pasword};";
                return true;
            }
            else if (dbType.ToLower() == "PostgreSql".ToLower())
            {
                DbType = SqlSugar.DbType.PostgreSQL;
                ConnectionString = $"Server={host};Port=5432;Database={dbName};User Id={userName};Password={pasword};";
                return true;
            }
            else if (dbType.ToLower() == "Sqlite".ToLower())
            {
                DbType = SqlSugar.DbType.Sqlite;

                var template = Engine.CreateTemplate(dbName);
                template.Set("BaseDirectory", MyEnvironment.RootPath(""));
                var result = template.Render();

                ConnectionString = $"DataSource={result};";
                return true;
            }
            else
            {
                message = "不支持的数据库";
                return false;
            }
        }

        public static SqlSugarClient GetInstance()
        {
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = ConnectionString,
                DbType = DbType,
                IsAutoCloseConnection = true,
                //ConfigureExternalServices = new ConfigureExternalServices()
                //{
                //    SqlFuncServices = ExtMethods.GetExpMethods,
                //    DataInfoCacheService = new HttpRuntimeCache()
                //}
            });
            //用来打印Sql方便你调式    
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                //if (DebugMode)
                {
                    var serializeObject = sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
                    Console.WriteLine(serializeObject);
                    Console.WriteLine();
                }
            };
            return db;
        }

        public static void InitTables()
        {
            var db = GetInstance();
            db.DbMaintenance.CreateDatabase();
            var typeList = Assembly.GetExecutingAssembly().GetTypes().Where(it => it.FullName != null && it.IsClass && it.FullName.Contains("AutoShuntingSchedule.DAL.Models.Sys"));
            db.CodeFirst.InitTables(typeList.ToArray());
        }
    }
}
